//
// Created by EDY on 24-11-3.
//

#include "MysqlManager.h"

bool MysqlManager::init(){
    mysql_init(m_Mysql);
    if(!mysql_real_connect(m_Mysql, m_host, m_username, m_password, m_database, m_port,m_unix_socket,m_clientflag)){
        return false;
    }

    if(mysql_set_character_set(m_Mysql, "utf8")){
        cerr << "mysql_set_character_set error" << mysql_error(m_Mysql) << endl;
        return false;
    }
    return true;
}

void MysqlManager::freeConnect(){
    mysql_free_result(m_SelectResult);
    mysql_close(m_Mysql);
}

bool MysqlManager::runSql(const char* sql){
    if(mysql_query(m_Mysql, sql)){
        cerr << "mysql_query error" << mysql_error(m_Mysql) << endl;
        return false;
    }else{
        cout << "runSql success" << endl;
    }

    return true;
}

bool MysqlManager::selectData(const char* sql){
    if(mysql_query(m_Mysql, sql)){
        cerr << "mysql_query error" << mysql_error(m_Mysql) << endl;
        return false;
    }else{
        cout << sql << " select success" << endl;
    }

    m_SelectResult = mysql_store_result(m_Mysql);
    return true;
}

map<string, Friend> MysqlManager::loadContact(){
    const char* sql = "select * from friend";
    selectData(sql);
    map<string, Friend> contact{};
    Friend fri{};
    MYSQL_ROW row;

    while(row = mysql_fetch_row(m_SelectResult)){
        fri.id = atoi(row[0]);
        strcpy(fri.name, row[1]);
        strcpy(fri.phone,row[2]);
        strcpy(fri.address,row[3]);
        contact[fri.phone] = fri;
    }

    return contact;
}


void MysqlManager::saveContact(const map<string, Friend>& contact){
    const char* clearFriendTable = "delete from friend";

    if(!runSql(clearFriendTable)){
        cout << "clearFriendTable error" << mysql_error(m_Mysql) << endl;
        return;
    }else{
        cout << "clearFriendTable success" << endl;
    }

    const char* insertFridendSql = "insert into friend(name,phone,address) values (?,?,?)";
    MYSQL_STMT* stmt = mysql_stmt_init(m_Mysql);

    if(!stmt){
        cout << "mysql_stmt_init error" << mysql_error(m_Mysql) << endl;
        return;
    }else{
        cout << "mysql_stmt_init success" << endl;
    }

    if(mysql_stmt_prepare(stmt, insertFridendSql, strlen(insertFridendSql))){
        cout << "mysql_stmt_prepare error" << mysql_error(m_Mysql) << endl;
        mysql_stmt_close(stmt);
        return;
    }else{
        cout << "mysql_stmt_prepare success" << endl;
    }

    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    char name[20];
    char phone[20];
    char address[20];
    unsigned long name_len, phone_len, address_len;
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = name;
    bind[0].buffer_length = sizeof(name);
    bind[0].length = &name_len;

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = phone;
    bind[1].buffer_length = sizeof(phone);
    bind[1].length = &phone_len;

    bind[2].buffer_type = MYSQL_TYPE_STRING;
    bind[2].buffer = address;
    bind[2].buffer_length = sizeof(address);
    bind[2].length = &address_len;

    if(mysql_stmt_bind_param(stmt, bind)){
        cout << "mysql_stmt_bind_param error" << mysql_error(m_Mysql) << endl;
        return;
    }else{
        cout << "mysql_stmt_bind_param success" << endl;
    }

    for(const auto& pair:contact){
        const Friend& fri = pair.second;

        strncpy(name, fri.name, sizeof(name));
        name_len = strlen(fri.name);
        strncpy(phone, fri.phone, sizeof(phone));
        phone_len = strlen(fri.phone);
        strncpy(address, fri.address, sizeof(address));
        address_len = strlen(fri.address);
        if(mysql_stmt_execute(stmt)){
            cout << "mysql_stmt_execute error" << mysql_error(m_Mysql) << endl;
        }else{
            cout << "insert success" << endl;
        }
    }
    mysql_stmt_close(stmt);
}





